import GlobalService from './global/GlobalService'
import { usePermission, baseUrl } from '@/common/config'

let instance = null

const backUrl = baseUrl.split('/')
  .filter(e => !!e)
  .fill('..')
  .join('/')

class Service extends GlobalService {
  constructor() {
    super()
    return instance || this
  }

  static getInstance() {
    if (!instance) {
      instance = new Service()
    }
    return instance
  }

  /**
   * 权限系统 获取用户信息
   * 获取用户信息
   * @returns
   */
  getUserInfo = async () => {
    if (usePermission) {
      return this.get(`${backUrl}/jwcloud/system/findCurrentUserDetail`)
    } else {
      return { id: 1, name: 'admin' }
    }
  }

  /**
   * 权限系统 获取权限资源列表
   * @param systemId 系统id
   * 获取权限列表
   */
  getPermissionResource = async (systemId) => {
    if (usePermission) {
      return this.post(`${backUrl}/jwcloud/system/findResource`, { path: systemId })
    } else {
      return []
    }
  }

  /**
   * 获取系统中所有需要的维表
   * @returns {Promise<void>}
   */
  getAllDictionary = async () => {
    return await this.get('/system/drop_down_list_all')
  }
}

export default Service.getInstance()
